#include <types.h>
#include <sys_config.h>
#include <api/libtsi/si_types.h>

#include <api/libsi/si_module.h>
#include <api/libsi/si_nit.h>
#include <api/libsi/si_utility.h>
#include <api/libsi/si_desc_sat_delivery.h>

#define SDD_DEBUG_LEVEL			0
#if (SDD_DEBUG_LEVEL>0)
#include <api/libc/printf.h>
#define SDD_PRINTF			libc_printf
#else
#define SDD_PRINTF(...)			do{}while(0)
#endif



INT32 si_on_sat_delivery_desc(UINT8 tag, UINT8 length, UINT8 *data, void *priv)
{
	UINT16 polar;
	struct satellite_delivery_system_descriptor *sat =
		(struct satellite_delivery_system_descriptor *)(data-2);
	struct nit_section_info *n_info = (struct nit_section_info *)priv;
	TP_INFO *tp_info;

	if (n_info->xp_nr==PSI_MODULE_MAX_TP_NUMBER) {
		SDD_PRINTF("%s: maximum xponder collected!\n", __FUNCTION__);
		return SI_SUCCESS;
	}
	
	tp_info	= &n_info->xp[n_info->xp_nr++];
	polar = sat->polarization;

	/*
	 * AV require us to support L/R circle modulation for DVB-S.
	 *
	 * The following patch code is based on an un-official forum article:
	 * http://www.sat-china.com/vbb/showthread.php?threadid=19934
	 * that make the assumption that in voltage control,
	 *  L_CIRCLE is similar to HORIZONAL,
	 *  R_CIRCLE is similar to VIRTICAL,
	 *
	 * I didn't perform any detail verification, so we acctually need more 
	 * official document/theory to confirm this patch.
	 *
	 *			- Zhengdao Li.
	 */
#ifndef COMBOUI	 
	tp_info->s_info.polarity = polar<SAT_POL_CIRCLE_LEFT? polar: polar-2;
	
	tp_info->s_info.tsid = n_info->tsid;
	tp_info->s_info.onid = n_info->onid;

	tp_info->s_info.frequency = bcd2integer(sat->frequency, 8, 0)/100;
	tp_info->s_info.position = bcd2integer(sat->orbital_position, 4, 0);
	tp_info->s_info.symbol_rate = bcd2integer(sat->symbol_rate, 7, 0)/10;
	
	if (!sat->west_east_flag) {
		tp_info->s_info.position = 3600 - tp_info->s_info.position;
	}
	
	/*
	 * Arion require us to support FEC distinguish.
	 *
	 * I don't know why they need this, but sure we could support it.
	 *
	 *			- Zhengdao Li
	 */
	tp_info->s_info.FEC_inner = sat->FEC_inner;
	
	SDD_PRINTF("*freq: %d\n", tp_info->s_info.frequency);
	SDD_PRINTF("*symbol_rate: %d\n", tp_info->s_info.symbol_rate);
	SDD_PRINTF("*position: %d\n", tp_info->s_info.position);
	SDD_PRINTF("*FEC_inner: %d\n", tp_info->s_info.FEC_inner);
#else
	tp_info->combo_info.polarity = polar<SAT_POL_CIRCLE_LEFT? polar: polar-2;
	
	tp_info->combo_info.tsid = n_info->tsid;
	tp_info->combo_info.onid = n_info->onid;

	tp_info->combo_info.frequency = bcd2integer(sat->frequency, 8, 0)/100;
	tp_info->combo_info.position = bcd2integer(sat->orbital_position, 4, 0);
	tp_info->combo_info.symbol_rate = bcd2integer(sat->symbol_rate, 7, 0)/10;
	
	if (!sat->west_east_flag) {
		tp_info->combo_info.position = 3600 - tp_info->combo_info.position;
	}
	
	/*
	 * Arion require us to support FEC distinguish.
	 *
	 * I don't know why they need this, but sure we could support it.
	 *
	 *			- Zhengdao Li
	 */
	tp_info->combo_info.FEC_inner = sat->FEC_inner;
	
	SDD_PRINTF("*freq: %d\n", tp_info->combo_info.frequency);
	SDD_PRINTF("*symbol_rate: %d\n", tp_info->combo_info.symbol_rate);
	SDD_PRINTF("*position: %d\n", tp_info->combo_info.position);
	SDD_PRINTF("*FEC_inner: %d\n", tp_info->combo_info.FEC_inner);
#endif	
	return SI_SUCCESS;
}

